package com.zc.angelica.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.Account;
import com.zc.angelica.entity.MenuInfo;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.mapper.RoleMenuInfoMapper;
import com.zc.angelica.entity.RoleMenuInfo;
import com.zc.angelica.service.MenuInfoService;
import com.zc.angelica.service.RoleMenuInfoService;
import com.zc.angelica.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色菜单表(RoleMenuInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-04-05 23:54:04
 */
@Service
public class RoleMenuInfoServiceImpl extends ServiceImpl<RoleMenuInfoMapper, RoleMenuInfo> implements RoleMenuInfoService {


    @Autowired
    private MenuInfoService menuInfoService;

    //TODO 增加缓存

    @Override
    public ResponseData bindMenu(RoleMenuInfo roleMenuInfo) {
        if (roleMenuInfo == null || roleMenuInfo.getMenuId() == null || roleMenuInfo.getRoleId() == null || roleMenuInfo.getRoleCode() == null || roleMenuInfo.getMenuCode() == null) {
            return ResponseData.error("请求参数不能为空");
        }

        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo != null) {
            roleMenuInfo.setAccountId(accountInfo.getId());
            boolean save = this.save(roleMenuInfo);
            if (save) {
                return ResponseData.success("绑定成功");
            }
            return ResponseData.error("绑定失败");
        }
        return ResponseData.error("没有查询到用户信息，请重新登录再试");
    }

    @Override
    public ResponseData unboundMenu(Long roleId, Long menuId) {
        if (roleId == null || menuId == null) {
            return ResponseData.error("请求参数不能为空");
        }
        QueryWrapper<RoleMenuInfo> roleMenuInfoQueryWrapper = new QueryWrapper<>();
        roleMenuInfoQueryWrapper.eq("menu_id", menuId).eq("role_id", roleId);
        int delete = this.baseMapper.delete(roleMenuInfoQueryWrapper);
        if (delete == 1) {
            return ResponseData.success("解绑成功");
        }
        return ResponseData.error("解绑失败");
    }

    @Override
    public ResponseData getAllMenu() {
        List<RoleMenuInfo> bindMenu = this.list();
        List<MenuInfo> menu = menuInfoService.list();
        if (bindMenu.size() > 0) {
            List<Long> bindMenuId = bindMenu.stream().map(RoleMenuInfo::getMenuId).collect(Collectors.toList());
            menu.stream().forEach(item -> {
                if (bindMenuId.contains(item.getId())) {
                    item.setBinding(2);
                }
            });
        }
        //TODO 构建树
        return ResponseData.success(menu);
    }

    @Override
    public List<Tree<Object>> getAllbindMenu() {
        List<RoleMenuInfo> bindMenu = this.list();
        List<MenuInfo> allMenu = menuInfoService.list();
        List<MenuInfo> myMenu = new ArrayList<>();
        if (bindMenu.size() > 0) {
            List<Long> bindMenuId = bindMenu.stream().map(RoleMenuInfo::getMenuId).collect(Collectors.toList());
            allMenu.forEach(item -> {
                if (bindMenuId.contains(item.getId())) {
                    myMenu.add(item);
                }
            });
        }
        List<TreeNode<Object>> nodeList = CollUtil.newArrayList();

        for (MenuInfo menu : myMenu) {
            Map<String, Object> map = new HashMap<>();
            map.put("menuCode",menu.getMenuCode());
            map.put("menuType",menu.getMenuType());
            map.put("menuUrl",menu.getMenuUrl());
            map.put("manuIcon",menu.getManuIcon());
            map.put("parentCode",menu.getParentCode());
            map.put("platformCode",menu.getPlatformCode());
            TreeNode<Object> node = new TreeNode<>()
                    .setId(menu.getId())
                    .setParentId(menu.getParentId())
                    .setName(menu.getMenuName())
                    .setWeight(menu.getMenuSort())
                    .setExtra(map);
            nodeList.add(node);
        }
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<Tree<Object>> treeNodes = TreeUtil.build(nodeList, 0L, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId());
                    tree.setParentId(treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("menuCode", treeNode.getExtra().getOrDefault("menuCode", null));
                    tree.putExtra("menuType", treeNode.getExtra().getOrDefault("menuType", null));
                    tree.putExtra("menuUrl", treeNode.getExtra().getOrDefault("menuUrl", null));
                    tree.putExtra("manuIcon", treeNode.getExtra().getOrDefault("manuIcon", null));
                    tree.putExtra("parentCode", treeNode.getExtra().getOrDefault("parentCode", null));
                    tree.putExtra("platformCode", treeNode.getExtra().getOrDefault("platformCode", null));
                });
        return treeNodes;
    }
}

